package com.suep.teacher_manage.controller;

import com.suep.teacher_manage.application.dto.AddTeacherRequestDto;
import com.suep.teacher_manage.application.dto.TeacherDto;
import com.suep.teacher_manage.application.dto.UpdateTeacherRequestDto;
import com.suep.teacher_manage.application.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("api/teacher")
public class TeacherController {
    private final TeacherService teacherService;

    /**
     * Query by PathVariable or RequestParam
     *
     * @param id teacher id
     * @return teacher
     */
    //swagger annotation
    @Operation(
                summary = "根据教师工号查询教师信息",
                description = "根据教师工号查询单个教师",
                parameters = {
                    @Parameter(name = "id", description = "教师工号", required = true, examples = {
                        @ExampleObject(value = "20220001", description = "教师工号为20220001的教师信息"),
                    })
                },
                responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取教师信息",content = @Content(schema = @Schema(implementation = TeacherDto.class))),
                        @ApiResponse(responseCode = "404",description = "教师信息不存在",content = @Content)
                }
    )
    //spring annotation
    //以id获取单个老师信息
    @GetMapping("/get/{id}")
    public ResponseEntity<TeacherDto> findTeacherById(@PathVariable(name = "id") String id) {
        var teacher = teacherService.getTeacherById(id);
        if (teacher == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(teacher);
    }
    //获取所有老师信息
    @GetMapping("/get/all")
    public List<TeacherDto> findAllTeacher() {
        return this.teacherService.getAllTeachers();
    }
    //插入新老师信息
    @Operation(summary = "新增教师信息", description = "新增一个教师信息")
    @PostMapping("/add")
    public ResponseEntity<?> addTeacher(
            @RequestBody AddTeacherRequestDto addTeacherRequestDto,
            BindingResult bindingResult){
        //request validation
        if(bindingResult.hasErrors()){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(this.buildValidationError(bindingResult));
        }

        var result =this.teacherService.addTeacher(addTeacherRequestDto);
        if(result!= null){
            System.out.println("add teacher result: " + result);
            return ResponseEntity.ok().build();
        }else{
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    private List<String> buildValidationError(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return bindingResult.getFieldErrors().stream()
                    .map(error -> error.getField() + ": " + error.getDefaultMessage()).toList();
        }
        return null;
    }
    //删除指定老师
    @Operation(summary = "删除教师信息", description = "删除一个教师信息")
    @DeleteMapping(path="/delete/{id}")
    public void removeTeacher(@PathVariable(name = "id") String id) {
        this.teacherService.removeTeacher(id);
    }
    //更新指定老师信息
    @Operation(summary = "更新教师信息", description = "更新一个教师信息")
    @PostMapping(path="/update/{id}")
    public ResponseEntity<?> updateTeacher(
            @PathVariable(name = "id") String id,
            @RequestBody UpdateTeacherRequestDto updateTeacherRequestDto,
            BindingResult bindingResult){
        //request validation
        if(bindingResult.hasErrors()){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(this.buildValidationError(bindingResult));
        }
        var result =this.teacherService.updateTeacher(id,updateTeacherRequestDto);
        if(result!= null){
            System.out.println("update teacher result: " + result);
            return ResponseEntity.ok().build();
        }else{
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

}
